package com.icarsaas.saas.master.model;

import com.icarsaas.saas.common.enums.StatusEnum;
import com.icarsaas.saas.component.base.DataEntity;
import lombok.Data;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.io.Serializable;

/**
 * 记录租户的Schema信息
 * 
 * @author chlingm
 *
 */
@Entity @Data
@Table(name = "master_tenant")
@EntityListeners(AuditingEntityListener.class)
@DynamicInsert @DynamicUpdate
@org.hibernate.annotations.Table(appliesTo = "master_tenant",comment="租户表")
public class MasterTenant extends DataEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    /**
     * 租户编号
     */
    @Column(name = "tenant_id", columnDefinition="varchar(30) COMMENT '租户ID'")
    private String tenantId;
    @Column(name = "tenant_name", columnDefinition="varchar(200) COMMENT '租户名称'")
    private String tenantName; //租户名称
    @Column(name = "short_name", columnDefinition="varchar(200) COMMENT '租户简称'")
    private String shortName; //租户简称
    /**
     * 数据库连接地址
     */
    @Column(name = "url", columnDefinition="varchar(256) COMMENT '数据库连接地址'")
    private String url;
    /**
     * 数据库用户
     */
    @Size(max = 30)
    @Column(name = "username", columnDefinition="varchar(30) COMMENT '数据库用户'")
    private String username;
   /**
    * 数据库密码
    */
    @Size(max = 36)
    @Column(name = "password", columnDefinition="varchar(36) COMMENT '数据库密码'")
    private String password;
    @Column(name = "connection_timeout", columnDefinition="bigint COMMENT '数据库连接超时时间,默认30S'")
    private long connectionTimeout = 30000;
    @Column(name = "idle_timeout", columnDefinition="bigint COMMENT '空闲连接存活最大时间,默认600000S'")
    private long idleTimeout = 300000;
    @Column(name = "minimum_idle", columnDefinition="int COMMENT '最小空闲连接数量'")
    private int minimumIdle = 1;
    @Column(name = "maximum_pool_size", columnDefinition="int COMMENT '最大连接数量'")
    private int maximumPoolSize = 10;

    /** 数据状态 */
    @Column(name = "status", columnDefinition="tinyint COMMENT '数据状态'")
    private Byte status = StatusEnum.OK.getCode();
    /** 是否初始化 */
    @Column(name = "init", columnDefinition="tinyint COMMENT '是否初始化'")
    private Byte init = new Byte(NO);

    @JoinColumn(name = "detail_id",referencedColumnName = "id")
    @OneToOne(cascade = {CascadeType.REMOVE},fetch = FetchType.LAZY, mappedBy = "masterTenant")
    private MasterTenantDetail masterTenantDetail;

    /**
     * 乐观锁
     */
    @Version
    private int version = 0;
    @Transient private String sqlCreate;//绑定建库的sql
    @Transient private String sqlInit;//绑定初始数据的sql
    @Transient private String sqlGrant;//绑定授权的sql

}